package com.settlement.system.service.impl;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblWpAuditCloseAccountMapper;
import com.settlement.system.mapper.TblWpAuditStaticDtlMapper;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.entity.common.SystemUser;
import com.settlement.system.model.entity.common.TblWpAuditCloseAccount;
import com.settlement.system.service.ITblWpAuditCloseAccountService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 唯品稽核关账设置 服务实现类
 *
 * @author xiaochuan
 * @since 2020-11-19
 */
@Slf4j
@Service
public class TblWpAuditCloseAccountServiceImpl extends BaseServiceImpl implements ITblWpAuditCloseAccountService {

    @Resource
    private TblWpAuditCloseAccountMapper tblWpAuditCloseAccountMapper;

    @Resource
    private TblWpAuditStaticDtlMapper tblWpAuditStaticDtlMapper;

    @Override
    public BaseMapper init() {
        return tblWpAuditCloseAccountMapper;
    }

    @Override
    public TblWpAuditCloseAccount getByShopNo(String shopNo) throws ServiceException {

        try {
            return tblWpAuditCloseAccountMapper.getByShopNo(shopNo);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Date getMaxInitialDateByShopNo(String shopNo) throws ServiceException {

        try {
            return tblWpAuditCloseAccountMapper.getMaxInitialDateByShopNo(shopNo);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public <ModelType> Integer insert(ModelType modelType) throws ServiceException {
        saveClose((TblWpAuditCloseAccount) modelType);
        return 1;
    }

    @Override
    public <ModelType> void save(List<ModelType> list) throws ServiceException {
        for (ModelType modelType : list) {
            saveClose((TblWpAuditCloseAccount) modelType);
        }
    }

    private void saveClose(TblWpAuditCloseAccount closeAccount) throws ServiceException {
        try {
            tblWpAuditCloseAccountMapper.insert(closeAccount);
        } catch (Exception ex) {
            if (ex.getCause() instanceof DuplicateKeyException || ex instanceof DuplicateKeyException) {
                String message = String.format("店铺编码：%s 已经存在", closeAccount.getShopNo());
                throw new ServiceException(message);
            }
            throw new ServiceException("系统异常");
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void closeAccount(List<String> ids, Date realCloseAccountDate) throws ServiceException {

        SysUserDetails user = SecurityUtils.getUser();;
        for (String id : ids) {
            try {
                while (true) {

                    TblWpAuditCloseAccount current = tblWpAuditCloseAccountMapper.getById(id);
                    if (current == null) {
                        throw new ServiceException("数据被删除不能更新");
                    }

                    if (realCloseAccountDate.compareTo(current.getInitialDate()) < 0) {
                        throw new ServiceException("锁帐日期不能小于初始日期");
                    }

                    current.setRealCloseAccountDate(realCloseAccountDate);
                    //如果回滚日期比实际日期大，就把回滚日期设为实际关账日期，如果小，就不用改了。
                    if (current.getNeedRollbackAccountData().compareTo(realCloseAccountDate) > 0) {
                        current.setNeedRollbackAccountData(realCloseAccountDate);
                    }

                    if (user != null) {
                        current.setUpdateUser(user.getUsername());
                    }

                    if (tblWpAuditCloseAccountMapper.updateRealCloseAccountDate(current) > 0) {
                        break;
                    }
                }
            } catch (ServiceException ex) {
                throw ex;
            } catch (Exception ex) {
                log.error("更新异常", ex);
                throw new ServiceException("系统异常", ex);
            }
        }
    }

    @Override
    public TblWpAuditCloseAccount getById(String id) throws ServiceException {
        try {
            return tblWpAuditCloseAccountMapper.getById(id);
        } catch (Exception ex) {
            throw new ServiceException("通过ID获取关账日异常", ex);
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void deleteClose(TblWpAuditCloseAccount closeAccount) throws ServiceException {
        try {
            tblWpAuditCloseAccountMapper.deleteById(closeAccount);
            tblWpAuditStaticDtlMapper.deleteByShopNo(closeAccount.getShopNo());
        } catch (Exception ex) {
            throw new ServiceException("删除关账日异常", ex);
        }
    }

}

